package com.cfm.bombercool 
{
	
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.*;
	/**
	 * 
	 * @author devninja
	 */
	public class Bomberman extends FlxSprite
	{
		private var playState:PlayState;
		public var maxBombs:int;
		public var power:int;
		
		private var noBombTime:Number;
		
		public function Bomberman(playState:PlayState, x:int, y:int) 
		{
			super(x, y);
			this.playState = playState;
			
			loadGraphic(playState.BombermanSheet, true, true, 16, 32, true);
			
			width = 14;
			height = 16;
			
			//Se debe mover para organizar el bounding.
			offset.x = 1;
			offset.y = 16;
			
			maxVelocity.x = 80;
			maxVelocity.y = 200;
			drag.x = maxVelocity.x * 7;
			
			
			addAnimation("idle-up", [0], 1, true);
			addAnimation("idle-right", [10], 1, true);
			addAnimation("idle-down", [20], 1, true);
			
			addAnimation("walk-up", [0, 1, 2], 6, true);
			addAnimation("walk-right", [10, 11, 12], 6, true);
			addAnimation("walk-down", [20, 21, 22], 6, true);
			
			if (FlxG.getPlugin(FlxControl) == null)
			{
				FlxG.addPlugin(new FlxControl);
			}
			
			FlxControl.create(this, FlxControlHandler.MOVEMENT_ACCELERATES, FlxControlHandler.STOPPING_DECELERATES, 1, true, false);

			FlxControl.player1.setCursorControl(true, true, true, true);
			
			FlxControl.player1.setMovementSpeed(400, 400, 100, 100, 400, 400);
			
			facing = FlxObject.DOWN;
			
			power = 1;
			maxBombs = 5;
			noBombTime = 0;
		}
		override public function update():void
		{
			super.update();
			
			if ( FlxG.keys.Z ) {
				dropBomb();
			}
			
			var state:String = "idle-";
			
			if ( velocity.x != 0 || velocity.y != 0 ) {
				state = "walk-";
			}
			
			switch(facing) {
				case FlxObject.UP:
					play(state + "up");
					break;
				case FlxObject.RIGHT:
					play(state + "right");
					break;
				case FlxObject.LEFT:
					play(state + "right");
					break;
				case FlxObject.DOWN:
					play(state + "down");
					break;
			}
			
			noBombTime -= FlxG.elapsed;
		}
		
		public function dropBomb():void
		{
			if( noBombTime <= 0 ){
				playState.dropBomb();
				noBombTime = 0.25;
			}
		}
	}

}